<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>工资计算器</title>
</head>
<body>
    <div style="margin: 50px auto; text-align: center;">
        <span>
            <h3>初始数组</h3>
            <input type="text" id= "arr">
        </span>
        <span>
            <h3>排序规则</h3>
            <input type="text" id= "rule">
        </span>
        <br>
        <button id = "sort">排序</button>
        <span>
            <h3>计算结果</h3>
            <div type="text" id= "result"></div>
        </span>
    </div>
    <script>
        function $(id) {
            return document.getElementById(id);
        }
        
        let sort = $("sort");
        let result = $("result");
        let rule = $("rule");
        let arr = $("arr");
        let array = [
            {
                id: 0,
                state: -1,
            },
            {
                id: 1,
                state: 1,
            },
            {
                id: 2,
                state: 1,
            },
            {
                id: 3,
                state: 0,
            },
            {
                id: 4,
                state: -1,
            },
            {
                id: 5,
                state: 0,
            }
        ]
        sort.addEventListener("click",() => {
            array.sort((a, b) => {
                if(a.state > 0 && b.state <= 0) {
                    return -1;
                } else {
                    return b.state - a.state;
                }
            })
            
            result.innerHTML = array;
        }, false);
    </script>
</body>
</html>